package com.niit.view;

import com.niit.entity.Users;
import com.niit.service.UsersService;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class UsersView {
    // ANSI颜色代码
    final String RESET = "\u001B[0m";
    final String RED = "\u001B[31m";
    final String GREEN = "\u001B[32m";
    final String YELLOW = "\u001B[33m";
    final String BLUE = "\u001B[34m";
    final String PURPLE = "\u001B[35m";
    final String CYAN = "\u001B[36m";
    final String BOLD = "\u001B[1m";
    final String BG_BLUE = "\u001B[44m";
    final String BG_GREEN = "\u001B[42m";
    UsersService usersService = new UsersService();
//  如何定义一个方法：
//    方法要用来做什么？---显示欢迎页面
//    方法要给谁访问？---public
//    方法执行之后有什么返回值？---没有--void
//    方法执行需要传入什么参数？---没有
//    显示欢迎页面的方法
    public void showWelcomeView() throws InterruptedException, SQLException {
        // 清屏（不一定在所有终端都有效）
        System.out.print("\033[H\033[2J");
        System.out.flush();
        showTitle();
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");
        System.out.println("请输入数字选择要进行的操作（1~3）");
        Scanner sc = new Scanner(System.in);
        String num = sc.next();
        switch (num){
            case "1":
                System.out.println("正在进入登录页面...");
                Thread.sleep(500);
                showLoginView(sc);
                break;
            case "2":
                System.out.println("正在进入注册页面...");
                Thread.sleep(500);
                showRegisterView(sc);
                break;
            case "3":
                System.out.println("正在退出系统...");
                Thread.sleep(500);
                break;
        }

    }

    public void showLoginView(Scanner sc) throws SQLException {
        System.out.println("*****************************登录页面*******************************");
        System.out.println("请输入用户名：");
        String phoneNum = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        Boolean login = usersService.login(phoneNum, password);
        if(login){
            System.out.println("登录成功！");
            showMainView(sc);
        }else {
            System.out.println("登录失败！");
            showLoginView(sc);
        }


    }
    public void showRegisterView(Scanner sc) throws SQLException {
        System.out.println("*****************************注册页面*******************************");
        System.out.println("请输入用户名：");
        String phoneNum = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        Boolean login = usersService.register(phoneNum, password);
        if(login){
            showLoginView(sc);
        }else {
            System.out.println("注册失败！");
            showRegisterView(sc);
        }


    }

    public void showMainView(Scanner sc) throws SQLException {
        showTitle();
        System.out.println("请输数字选择要进入的模块：");
        System.out.println("1.用户管理");
        System.out.println("2.订单管理");
        System.out.println("3.书本管理");
        System.out.println("4.退出登录");
        String menu = sc.next();
        switch (menu){
            case "1":
                showUserView(sc);
                //TODO 进入用户管理页面
                break;
            case "2":
                //TODO 进入订单管理页面
                break;
            case "3":
                //TODO 进入书本管理页面
                break;

            default:
                System.out.println("数字输入错误请重新输入");
                showMainView(sc);

        }


    }

    public void showUserView(Scanner sc) throws SQLException {
        System.out.println("*******************用户管理页面*******************");
        List<Users> usersList = usersService.listUsers(null, null, null, 1, 10);
        System.out.println("用户ID\t 用户名\t性别\t生日\t创建时间\t角色");
        for (Users users : usersList) {
            System.out.println(users);
        }
        System.out.println("请输入数字选择要进行的操作：");
        System.out.println("1.数据查询（多条件分页查询）");
        System.out.println("2.修改用户");
        System.out.println("3.删除用户");
        System.out.println("4.添加用户");
        String num = sc.next();
        switch (num){
            case "1":
                showListUsersView(sc);
                break;
            case "2":
                //TODO showListUsersView(sc);
                break;
            case "3":
                //TODO  showListUsersView(sc);
                break;
            case "4":
                //TODO  showListUsersView(sc);
                break;
        }
    }
    int page = 1;
    String phoneNum = null;
    Integer gender = null;
    Integer role = null;
    public void showListUsersView(Scanner sc) throws SQLException {
        System.out.println("******************用户查询页面*******************");

        listUsers(phoneNum,gender,role,page,10);
        System.out.println("请输入数字选择要进行的操作：");
        System.out.println("1.条件查询");
        System.out.println("2.下一页");
        System.out.println("3.上一页");
        System.out.println("4.清空筛选");
        System.out.println("5.返回上一级页面");
        String num = sc.next();
        switch (num){
            case "1":
                System.out.println("请输入用户名（可以模糊搜索）：");
                phoneNum = sc.next();
                System.out.println("请输性别1男2女：");
                gender = sc.nextInt();
                System.out.println("请输入角色：");
                role = sc.nextInt();
                page = 1;
                showListUsersView(sc);
            case "2":
                page++;
                showListUsersView(sc);
                break;
            case "3":
                if(page==1){
                    System.out.println("已经是第一页");
                    break;
                }
                page--;
                showListUsersView(sc);
                break;
            case "4":
                page = 1;
                phoneNum = null;
                gender = null;
                role = null;
                break;
            case "5":
                showUserView(sc);
                break;
        }
    }
    public void listUsers(String phoneNum, Integer gender, Integer role , Integer page, Integer pageSize) throws SQLException {
        List<Users> usersList2 = usersService.listUsers(phoneNum, gender, role, page, pageSize);
        System.out.println("用户ID\t 用户名\t性别\t生日\t创建时间\t角色");
        for (Users users : usersList2) {
            System.out.println(users);
        }
    }

    public void showTitle(){
        // 顶部艺术字标题
        System.out.println(BOLD + BLUE + "╔════════════════════════════════════════════════════════╗" + RESET);
        System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██████╗  ██████╗  ██████╗ ██╗  ██╗███████╗    " + RESET + BOLD + BLUE + "║" + RESET);
        System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██╔══██╗██╔═══██╗██╔═══██╗██║ ██╔╝██╔════╝    " + RESET + BOLD + BLUE + "║" + RESET);
        System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██████╔╝██║   ██║██║   ██║█████╔╝ █████╗      " + RESET + BOLD + BLUE + "║" + RESET);
        System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██╔══██╗██║   ██║██║   ██║██╔═██╗ ██╔══╝      " + RESET + BOLD + BLUE + "║" + RESET);
        System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██████╔╝╚██████╔╝╚██████╔╝██║  ██╗███████╗    " + RESET + BOLD + BLUE + "║" + RESET);
        System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ╚═════╝  ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝    " + RESET + BOLD + BLUE + "║" + RESET);
        System.out.println(BOLD + BLUE + "╠════════════════════════════════════════════════════════╣" + RESET);

        // 欢迎信息
        System.out.println(BOLD + BLUE + "║" + RESET + "       " + CYAN + "欢迎使用书香阁图书管理系统 - V2.0" + RESET + "       " + BOLD + BLUE + "║" + RESET);
        System.out.println(BOLD + BLUE + "║" + RESET + "       " + YELLOW + "探索知识海洋，发现阅读乐趣" + RESET + "       " + BOLD + BLUE + "║" + RESET);
        System.out.println(BOLD + BLUE + "╠════════════════════════════════════════════════════════╣" + RESET);

    }



}
